<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
const isabled = ref()
isabled
const formModel = ref({
  id: '',
  vegetableid: '',
  output: '',
  salesamount: '',
  saledate: '',
  remarks: '',
  name: ''
  //crops: '',
  //area: '',
  //order: '',
  //address: ''
})
const rules = {
  name: [
    { required: true, message: '请输入菜地名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 40 个字符', trigger: 'blur' }
  ],
  output: [{ required: true, message: '请输入产能', trigger: 'blur' }]
}
const form = ref()
const emit = defineEmits(['success'])
import {
  plantingoutputinfoAddService,
  plantingoutputinfoUpService
} from '@/api/admin/plantingoutput'

const sureQuire = async () => {
  await form.value.validate()
  const isEdit = formModel.value.id
  if (isEdit) {
    plantingoutputinfoUpService(formModel.value)
      .then((response) => {
        if (response.data.status === 200) {
          ElMessage.success(response.data.message)
          dialogVisible.value = false
          emit('success')
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  } else {
    plantingoutputinfoAddService(formModel.value)
      .then((response) => {
        if (response.data.status == '200') {
          dialogVisible.value = false
          emit('success')
          ElMessage.success(response.data.message)
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  }
}
const NoQuire = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClose = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClearValidate = () => {}

//
const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row } //添加的话，重置表单内容
}
// 处理日期变化事件
defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '修改' : '添加'"
    width="550"
    :before-close="handleClose"
  >
    <el-form
      class="el-form"
      :rules="rules"
      ref="form"
      :model="formModel"
      :before-close="handleClearValidate"
    >
      <el-form-item prop="name" label="菜地名称" required="true">
        <el-input
          v-model="formModel.name"
          :prefix-icon="User"
          placeholder="请输入"
          style="width: 450px"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="output"
        label="产&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;能"
        required="true"
      >
        <el-input
          v-model="formModel.output"
          :prefix-icon="User"
          placeholder="请输入"
          style="width: 175px"
          :disabled="isabled"
        ></el-input>
      </el-form-item>

      <el-form-item prop="saledate" label="出售时间">
        <el-date-picker
          v-model="formModel.saledate"
          type="date"
          value-format="YYYY-MM-DD"
          :size="size"
          @change="handleDateChange"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item prop="salesamount" label="&nbsp;&nbsp;售出情况">
        <el-input
          v-model="formModel.salesamount"
          :prefix-icon="User"
          type="textarea"
          min="1"
          :autosize="{ minRows: 2, maxRows: 4 }"
        ></el-input>
      </el-form-item>
      <el-form-item prop="remarks" label="备注">
        <el-input
          v-model="formModel.remarks"
          :prefix-icon="User"
          :autosize="{ minRows: 2, maxRows: 4 }"
          type="textarea"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="NoQuire">取消</el-button>
        <el-button type="primary" @click="sureQuire"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style>
.el-form {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}
</style>
